/**
 * 
 */
package gui.customControl;

import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import data.Player;
import data.Data.ActionType;
import data.Data.PlayerState;
import engine.GameEngine;

/**
 * @author Bogdan
 *
 */
public class ActionDialog extends JDialog 
{
	private static final long serialVersionUID = -3432918993849948636L;

	public ActionDialog(JFrame parent, Player player)
	{
		super(parent, "Player action", Dialog.ModalityType.DOCUMENT_MODAL);
		initializeComponents();
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		
		m_player = player;
		if(!m_player.hasBall() || GameEngine.getInstance().currentTeam().hasThrown() || m_player.hasActed())
			m_throwBtn.setEnabled(false);
		
		if(m_player.state() != PlayerState.PRONE)
			m_standBtn.setEnabled(false);
		
		if(m_player.currentMA() == 0 || m_player.state() != PlayerState.IN_PLAY)
			m_moveBtn.setEnabled(false);
		
		if(m_player.currentMA() != m_player.MA() || m_player.state() != PlayerState.IN_PLAY)
			m_blockBtn.setEnabled(false);
		
		if(m_player.hasActed() || m_player.state() != PlayerState.IN_PLAY ||
				GameEngine.getInstance().getPlayerTeam(m_player).hasBlitzed())
			m_blitzBtn.setEnabled(false);
		
		pack();
		setVisible(false);
	}
	
	private void initializeComponents()
	{
		m_moveBtn 	= new JButton("<html><u>M</u>ove</html>");
		m_throwBtn 	= new JButton("<html><u>T</u>hrow</html>");
		m_blockBtn	= new JButton("<html><u>B</u>lock</html>");
		m_blitzBtn	= new JButton("<html>Blit<u>z</u></html>");
		m_standBtn  = new JButton("<html><u>S</u>tand up</html>");
		
		ActionListener listener = new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				JButton source = (JButton)e.getSource();
				if(source.equals(m_moveBtn))
					m_action = ActionType.MOVE;
				else if(source.equals(m_throwBtn))
					m_action = ActionType.THROW;
				else if(source.equals(m_blockBtn))
					m_action = ActionType.BLOCK;
				else if(source.equals(m_blitzBtn))
					m_action = ActionType.BLITZ;
				else if(source.equals(m_standBtn))
					m_action = ActionType.STAND;
				
				GameEngine.getInstance().selectAction( m_player, m_action );				
				dispose();
			}
		};
		
		KeyListener keyListener = new KeyListener()
		{
			@Override
			public void keyPressed(KeyEvent arg0) {
				boolean closeWindow = true;
				boolean hitKey = true;
				
				switch(arg0.getKeyCode())
				{
				case KeyEvent.VK_M: m_action = ActionType.MOVE;  break;
				case KeyEvent.VK_T: m_action = ActionType.THROW; break;
				case KeyEvent.VK_B: m_action = ActionType.BLOCK; break;
				case KeyEvent.VK_Z: m_action = ActionType.BLITZ; break;
				case KeyEvent.VK_S:	m_action = ActionType.STAND; break;
				case KeyEvent.VK_ESCAPE: hitKey = false;         break;
				default: hitKey = false; closeWindow = false; break;
				}
				
				if( hitKey )
					GameEngine.getInstance().selectAction( m_player, m_action );
				
				if( closeWindow )
					dispose();
				
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		};

		m_moveBtn.addActionListener(listener);
		m_throwBtn.addActionListener(listener);
		m_blockBtn.addActionListener(listener);
		m_blitzBtn.addActionListener(listener);
		m_standBtn.addActionListener(listener);
		
		m_moveBtn.addKeyListener(keyListener);
		m_throwBtn.addKeyListener(keyListener);
		m_blockBtn.addKeyListener(keyListener);
		m_blitzBtn.addKeyListener(keyListener);
		m_standBtn.addKeyListener(keyListener);
		
		JPanel panel = (JPanel) getContentPane();
		panel.setLayout(new FlowLayout());
		panel.add(m_moveBtn);
		panel.add(m_throwBtn);
		panel.add(m_blockBtn);
		panel.add(m_blitzBtn);
		panel.add(m_standBtn);
	}
	
	/**
	 * Gets the selected action from the panel.
	 * @return Return the selected action.
	 */
	public ActionType getAction() 
	{
		setVisible(true);
		return m_action;
	}
	
	private ActionType m_action = ActionType.NONE;
	private Player     m_player = null;
	
	private JButton m_moveBtn;
	private JButton m_throwBtn;
	private JButton m_blockBtn;
	private JButton m_blitzBtn;
	private JButton m_standBtn;
}
